using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TaugeLibrary.Classes.Abstracts;
using TaugeFramework.Classes;
using TaugeLibrary.Interfaces;
using TaugeLibrary.Classes;
using TaugeLibrary.UserControls;

namespace TaugeFramework.Forms
{
    public partial class frmList : Form,IComplete
    {

        #region for dgList
        DataTable m_dtList;
        BindingSource m_bs = new BindingSource();
        Database m_db;
        string m_sql;
        #endregion

        ABusinessUnitManager m_unit_mgr;


        public ABusinessUnitManager UnitManager
        {
            get { return m_unit_mgr; }            
        }

        public frmList(ABusinessUnitManager p_unit_mgr)
        {
            InitializeComponent();


            #region dgList setup
            dgList.AllowUserToAddRows = false;
            dgList.AllowUserToDeleteRows = false;
            dgList.AllowUserToOrderColumns = true;
            dgList.AllowUserToResizeColumns = true;
            dgList.AllowUserToResizeRows = true;
            dgList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dgList.ReadOnly = true;
            dgList.MultiSelect = false;
            dgList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            #endregion


            //here
            #region var
            m_unit_mgr = p_unit_mgr;
            m_db = Program.Framework.getDatabase(m_unit_mgr.getDBType());
            m_sql = m_unit_mgr.getListSql();
            #endregion


            RefreshList();
        }

        public void RefreshList()
        {
                m_dtList = m_db.executeDataTable(m_sql);
                m_bs.DataSource = m_dtList;
                dgList.DataSource = m_bs;

                LibConstants.TidyGrid(dgList);

                string[] tmp = m_unit_mgr.getListHiddenColumn();
                if (tmp != null)
                {
                    foreach (string s in tmp)
                    {
                        dgList.Columns[s].Visible = false;
                    }
                }
                lblRecordCount.Text = dgList.Rows.Count.ToString("#,##0;(#,##0);0") + " Record(s)";
                //m_update_list = false;

        }


        #region INavigateable Members

        public int doMoveNext()
        {
            m_bs.MoveNext();
            return 0;
        }

        public int doMovePrev()
        {
            m_bs.MovePrevious();
            return 0;
        }

        public int doMoveFirst()
        {
                m_bs.MoveFirst();
            return 0;
        }

        public int doMoveLast()
        {
                m_bs.MoveLast();
            return 0;
        }

        public bool isNavigateable()
        {
            return true;
        }

        #endregion



        #region IPrintable Members

        public int doPrint()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool isPrintable()
        {
            return true;
        }

        #endregion

        #region ISaveAble Members

        public int doSave()
        {
            return 0;
        }

        public int doCancel()
        {
            this.Close();
            return 0;
        }

        public bool isSaveable()
        {
            return false;
        }

        #endregion

        #region IPostable Members

        public int doPosting()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int doUnPosting()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool isPostable()
        {
            return true;
        }

        public bool isUnPostable()
        {
            return true;
        }

        #endregion

        #region ISearchable Members

        public int doSearch()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool isSearchable()
        {
            return true;
        }

        #endregion

        #region IActionable Members

        public ABusinessUnit GetUnitFromList()
        {
            if (dgList.RowCount > 0)            
                return m_unit_mgr.GetUnitFromList(((DataRowView)dgList.SelectedRows[0].DataBoundItem).Row);
            else
                return null;
        }

        public int doQuery()
        {

                ABusinessUnit x = GetUnitFromList();
                if (x != null)
                {
                    frmBusinessUnit fb = new frmBusinessUnit(this, x, eViewMode.QUERY);
                    fb.MdiParent = this.MdiParent;
                    fb.Show();
                    return 0;
                }
                else
                {
                    MessageBox.Show("Data tidak ditemukan");
                    return -1;
                }

                            

            

        }

        public int doInsert()
        {
            ABusinessUnit x = m_unit_mgr.createUnit();
            frmBusinessUnit fb = new frmBusinessUnit(this, x, eViewMode.INSERT);
            fb.MdiParent = this.MdiParent;
            fb.Show();
            return 0;
        }

        public int doUpdate()
        {
            ABusinessUnit x = GetUnitFromList();
            if (x != null)
            {
                frmBusinessUnit fb = new frmBusinessUnit(this, x, eViewMode.UPDATE);
                fb.MdiParent = this.MdiParent;
                fb.Show();
                return 0;
            }
            else
            {
                MessageBox.Show("Data tidak ditemukan");
                return -1;
            }
            
        }

        public int doDelete()
        {
            ABusinessUnit x = GetUnitFromList();
            if (x != null)
            {
                frmBusinessUnit fb = new frmBusinessUnit(this, x, eViewMode.DELETE);
                fb.MdiParent = this.MdiParent;
                fb.Show();                
                return 0;
            }
            else
            {
                MessageBox.Show("Data tidak ditemukan");
                return -1;
            }
        }

        public bool isQueryable()
        {
            return true;
        }

        public bool isInsertable()
        {
            return true;
        }

        public bool isUpdateable()
        {
            return true;
        }

        public bool isDeleteable()
        {
            return true;
        }

        #endregion

        #region IRefreshAble Members

        public int doRefresh()
        {
            RefreshList();
            return 0;
        }

        public bool isRefreshable()
        {
            return true;
        }

        #endregion




        internal void CallBack(bool p_success, frmBusinessUnit p_form)
        {
            if (p_success)
                RefreshList();
            if (p_form.InitialViewMode == eViewMode.INSERT && p_success)
            {                
                ABusinessUnit x = m_unit_mgr.createUnit();
                p_form.WriteUI(x);
            }
            else if(p_form.InitialViewMode != eViewMode.QUERY)
            {
                p_form.Close();
                p_form.Dispose();             
            }
        }
    }
}